//index.js
const domain = require('../../config').domain
const article_list_url = require('../../config').article_list_url
//获取应用实例
var app = getApp()
var page_size = 10;
var page = 1;
var is_get = true;
Page({
  data: {
    domain: domain,
    settings: {},
    head_data: {},
    articles: {},
  },
  left_nav_btn: function (event) {
    this.setData({
        'head_data.left_nav_show': "show"
    })
  },
  left_nav_mask_btn: function (event) {
    this.setData({
      'head_data.left_nav_show': "hidden"
    })
  },
  

  onLoad: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that = this
    wx.getStorage({
      key: 'settings',
      success: function (res) {
        that.setData({
          settings: res.data
        });
      }
    })
    wx.getStorage({
      key: 'categorys',
      success: function (res) {
        that.setData({
         'head_data.categorys': res.data
        });
      }
    })

    //文章
    wx.request({
      url: article_list_url,
      data: {
        order: 'id desc',
        page_size: page_size,
        page: page
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          articles: res.data.data
        });
        if (res.data.current_page >= Math.ceil(res.data.total / res.data.per_page)) {
          is_get = false
        }
      }
    })

  },
  onReady: function () {
    wx.hideLoading()
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  onReachBottom: function () {
    var that = this
    if (is_get){
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      page++;
      wx.request({
        url: article_list_url,
        data: {
          order: 'id desc',
          page_size: page_size,
          page: page
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          var articles = that.data.articles;
          articles = articles.concat(res.data.data)
          that.setData({
            articles: articles
          });
          wx.hideLoading()
          if (res.data.current_page >= Math.ceil(res.data.total / res.data.per_page)) {
            is_get = false
          }
        }
      })
    }
    
  }



})
